from google.appengine.ext import db
import random

class Tags(db.Model):
	user = db.UserProperty()
	tag = db.StringProperty(multiline=True)
	name = db.TextProperty()
	height = db.IntegerProperty()
	width = db.IntegerProperty()



class Advertisers(db.Model):
	user = db.UserProperty()
	advertisername = db.TextProperty()
	date = db.DateTimeProperty(auto_now_add=True)
	url = db.TextProperty()

class Campaigns(db.Model):
	user = db.UserProperty()
	campaignname = db.TextProperty()
	date = db.DateTimeProperty(auto_now_add=True)

class Creatives(db.Model):
    user = db.UserProperty()
    data = db.BlobProperty()
    name = db.TextProperty()
    width = db.IntegerProperty()
    height = db.IntegerProperty()
    url = db.TextProperty()
    campaign = db.ReferenceProperty(Campaigns)

class CreativeStats(db.Model):
	creative = db.ReferenceProperty(Creatives)
	impressions = db.IntegerProperty(required=True, default = 0)


class ImpressionCounterShard(db.Model):
  """Shards for the counter"""
  count = db.IntegerProperty(required=True, default=0)  
  creative = db.ReferenceProperty(Creatives)
  date = db.DateTimeProperty(auto_now_add=True)
  ip = db.TextProperty()
  useragent = db.TextProperty()
  userid = db.TextProperty()

class ClickCounterShard(db.Model):
  """Shards for the counter"""
  count = db.IntegerProperty(required=True, default=0)  
  creative = db.ReferenceProperty(Creatives)
  date = db.DateTimeProperty(auto_now_add=True)
  ip = db.TextProperty()
  useragent = db.TextProperty()
  userid = db.TextProperty()
  click = db.TextProperty()

NUM_SHARDS = 20

def get_count():
  """Retrieve the value for a given sharded counter."""
  total = 0
  for counter in ImpressionCounterShard.all():
    total += counter.count
  return total

def increment_impression(creative, ipaddress, useragent, cookie_value):
  """Increment the value for a given sharded counter."""
  def txn():
    #index = random.randint(0, NUM_SHARDS - 1)
    #shard_name = "shard" + str(index)
    #imp_counter = ImpressionCounterShard.get_by_key_name(shard_name)
    #if imp_counter is None:
    #  imp_counter = ImpressionCounterShard(key_name=shard_name)
    imp_counter = ImpressionCounterShard()
    imp_counter.count += 1
    imp_counter.creative = creative
    imp_counter.ip = ipaddress
    imp_counter.useragent = useragent
    imp_counter.userid = cookie_value
    imp_counter.put()
  db.run_in_transaction(txn)

def increment_click(creative, ipaddress, useragent, cookie_value, click):
  """Increment the value for a given sharded counter."""
  def txn():
    # index = random.randint(0, NUM_SHARDS - 1)
    # shard_name = "shard" + str(index)
    # click_counter = ClickCounterShard.get_by_key_name(shard_name)
    # if click_counter is None:
    #   click_counter = ClickCounterShard(key_name=shard_name)
    click_counter = ClickCounterShard()
    click_counter.count += 1
    click_counter.creative = creative
    click_counter.ip = ipaddress
    click_counter.useragent = useragent
    click_counter.userid = cookie_value
    click_counter.click = click
    click_counter.put()
  db.run_in_transaction(txn)
